package com.gitee.srurl.core.config.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.gitee.srurl.core.util.login.LoginHelper;
import com.gitee.srurl.core.util.login.OperationUser;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;

/**
 * @author zxw
 * @date 2022/12/3
 */
public class AutoFillMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {

        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        OperationUser loginUser = LoginHelper.getLoginUserWithDefault();
        this.strictInsertFill(metaObject, "createId", Long.class, loginUser.getUserId());
        this.strictInsertFill(metaObject, "updateId", Long.class, loginUser.getUserId());
    }

    @Override
    public void updateFill(MetaObject metaObject) {

        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        OperationUser loginUser = LoginHelper.getLoginUserWithDefault();
        this.strictInsertFill(metaObject, "updateId", Long.class, loginUser.getUserId());
    }
}
